package com.atguigu.myjuc.demo;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 线程通信
 */
public class ProdConsumerDemo {
    public static void main(String[] args) {
        AirCondition airCondition = new AirCondition();

        new Thread(()->{
            for (int i = 0; i < 10; i++) {
                airCondition.incr();
            }
        },"A").start();

        new Thread(()->{
            for (int i = 0; i < 10; i++) {
                    airCondition.decr();
            }
        },"B").start();

        new Thread(()->{
            for (int i = 0; i < 10; i++) {
                    airCondition.incr();
            }
        },"C").start();

        new Thread(()->{
            for (int i = 0; i < 10; i++) {
                    airCondition.decr();
            }
        },"D").start();

    }
}

class AirCondition{
    private static final Logger log = LoggerFactory.getLogger(AirCondition.class); //资源类
    int number = 0;

    ReentrantLock lock = new ReentrantLock();
    Condition condition = lock.newCondition();
    public void incr(){
        lock.lock();
        try {
           while (number != 0){
               condition.await();
           }
            System.out.println(++number);
            condition.signalAll();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } finally {
            lock.unlock();
        }
    }

    public void decr(){
        lock.lock();
        try {
            while (number == 0){
                condition.await();
            }
            System.out.println(--number);
            condition.signalAll();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } finally {
            lock.unlock();
        }
    }
    /*Lock lock = new ReentrantLock();
    Condition condition = lock.newCondition();
    public void incr() throws InterruptedException {
        lock.lock();
        try {
            //判断
            while (number != 0){
                condition.await();
            }
            //干活
            System.out.println(Thread.currentThread().getName()+"\t"+ ++number);
            //通知.
            condition.signalAll();
        } finally {
            lock.unlock();
        }

    }

    public void decr() throws InterruptedException {
        lock.lock();
        try {
            //判断
            while (number == 0){
                condition.await();
            }
            //干活
            System.out.println(Thread.currentThread().getName()+"\t"+ --number);
            //通知.
            condition.signalAll();
        } finally {
            lock.unlock();
        }
    }

//    public synchronized void decr() throws InterruptedException {
//        //判断
//        while (number == 0){
//            wait();
//        }
//        //干活
//        System.out.println(Thread.currentThread().getName()+"\t"+ --number);
//        //通知
//        this.notifyAll();
//    }*/
}
